/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;



import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Dict;
import com.yuhanginfo.modules.sys.entity.Goods;
import com.yuhanginfo.modules.sys.entity.Materiel;
import com.yuhanginfo.modules.sys.entity.Vender;
import com.yuhanginfo.modules.sys.service.GoodsService;
import com.yuhanginfo.modules.sys.service.MaterielService;
import com.yuhanginfo.modules.sys.service.SystemService;
import com.yuhanginfo.modules.sys.service.VenderService;
import com.yuhanginfo.modules.sys.utils.DictUtils;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 平台商品上下架Controller
 * 
 * @author yinhao
 * @version 2017-09-25
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/goodsPlatformUpDown/")
public class GoodsPlatformUpDownController extends BaseController {

	private static final  Logger logger = LoggerFactory.getLogger(GoodsPlatformUpDownController.class);
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private MaterielService materielService;
	
	@Autowired
	private VenderService venderService;

	@Autowired
	SystemService systemService;
	
	
	@ModelAttribute
	public Goods get(@RequestParam(required = false) String id) {
		Goods entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = goodsService.get(id);
		}
		if (entity == null) {
			entity = new Goods();
		}
		return entity;
	}

	@RequiresPermissions("sys:goodsPlatformUpDown:view")
	@RequestMapping(value = { "list", "" })
	public String list(Goods goods, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Goods> page = goodsService.findPage(new Page<Goods>(request, response), goods);
		model.addAttribute("page", page);
		List<Dict> dictList = DictUtils.getDictList("goods_status");
		model.addAttribute("dictList",dictList);
		//制造商品牌
		List<Vender> venderList = venderService.findList(new Vender());
		model.addAttribute("venderList",venderList);
		model.addAttribute("user",UserUtils.getUser());
		return "modules/sys/goodsplatformupdown/goodsList";
	}

	@RequiresPermissions("sys:goodsPlatformUpDown:view")
	@RequestMapping(value = "form")
	public String form(Goods goods, Model model) {
		List<Materiel> materielList = materielService.findList(new Materiel());
		Goods resultGoods = new Goods();
		if( goods.getId() != null){
			resultGoods = goodsService.get(goods);
		}
		model.addAttribute("goods", resultGoods);
		model.addAttribute("materielList", materielList);
		return "modules/sys/goodsplatformupdown/goodsForm";
	}

	@RequiresPermissions("sys:goodsPlatformUpDown:edit")
	@RequestMapping(value = "up")
	public String up(Goods goods,String storeId, Model model, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		try {
			goodsService.up(goods,storeId);
		} catch (Exception ex) {
			logger.info("up",ex);
		}
		
		return this.list(goods, request, response, model);
	}
	
	@RequiresPermissions("sys:goodsPlatformUpDown:edit")
	@RequestMapping(value = "batchUp")
	public String batchUp(Goods goods,String id,String storeId, Model model, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		try {
			goodsService.batchUp(id,storeId);
		} catch (Exception ex) {
			logger.info("batchUp",ex);
		}
		
		return this.list(goods, request, response, model);
	}
	
	@RequiresPermissions("sys:goodsPlatformUpDown:edit")
	@RequestMapping(value = "down")
	public String down(Goods goods, Model model, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		try {
			goodsService.down(goods);
		} catch (Exception ex) {
			logger.info("down",ex);
		}
		addMessage(redirectAttributes, "保存平台商品成功");
		return this.list(goods, request, response, model);
	}
	
	@RequiresPermissions("sys:goodsPlatformUpDown:edit")
	@RequestMapping(value = "batchDown")
	public String batchDown(Goods goods,String[] arrayId ,  Model model, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		try {
			goodsService.batchDown(arrayId);
		} catch (Exception ex) {
			logger.info("batchDown",ex);
		}
		addMessage(redirectAttributes, "保存平台商品成功");
		return this.list(goods, request, response, model);
	}
	
	@RequiresPermissions("sys:goodsPlatformUpDown:edit")
	@RequestMapping(value = "delete")
	public String delete(Goods goods, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response, Model model) {
		goodsService.delete(goods);
		addMessage(redirectAttributes, "删除平台商品成功");
		return this.list(new Goods(), request, response, model);
	}

	@RequiresPermissions("sys:goodsPlatformUpDown:edit")
	@RequestMapping(value = "selectStoreList")
	public String selectAllStoreListByType(Goods goods,HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAllAttributes(goodsService.getStoreGoods(goods));
		return "modules/sys/tagStoreselectForList";
	}
	
}